﻿
@{
    ViewData["Title"] = "UseRoleAdd";
    Layout = "~/Views/Home/Main.cshtml";
}

<!DOCTYPE html>
<html>
<head>

</head>
<body>
    <h3>用户角色添加</h3>
    <div>
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">用户id:</label>
                <div class="layui-input-inline" id="userId">
                    <input type="checkbox" name="" title="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色id:</label>
                <div class="layui-input-inline">
                    <select id="roleId" lay-filter="aihao">
                        <option value=""></option>
                    </select>
                </div>
            </div>
        </form>
        <input type="button" value="确定" class="layui-btn layui-bg-blue" onclick="UseRoleAdd()" />
    </div>
    <script>

        layui.use(['form','table', 'dropdown'], function () {
            var table = layui.table;
            var dropdown = layui.dropdown;
            var form = layui.form;

            // 提交事件
            window.UserBang = function UserBang() {
                var token = localStorage.getItem("token")
                $.ajax({
                    url: '../User/UserBang',
                    type: 'get',
                    data: { token: token },
                    async: false,
                    //contentType: 'application/json',
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(res)
                        var count = ''
                        $.each(res.data, function () {
                            count += `<input type="checkbox" value=${this.id} name=${this.id} title="${this.name}">`
                        })

                        $("#userId").html(count)


                        form.render();
                        console.log($("#userId"))
                    }
                })
            }
            window.RoleBang = function RoleBang() {
                var token = localStorage.getItem("token")
                $.ajax({
                    url: '../User/RoleBang',
                    type: 'get',
                    data: { token: token },
                    async: false,
                    //contentType: 'application/json',
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(res)
                        var count = ''
                        $.each(res.data, function () {
                            count += `  <option value="${this.id}">${this.name}</option>">`
                        })

                        $("#roleId").html(count)
                        form.render();
                        console.log($("#roleId"))
                    }
                })
                window.UseRoleAdd = function UseRoleAdd() {
                  var userId = [];
                    roleId = $('#roleId').val();
                    var userId = document.getElementById("userId")
                    checkboxes = userId.getElementsByTagName('input')
                    var id = []
                    for (var i = 0; i < checkboxes.length; i++) {
                        var checkbox = checkboxes[i];
                        if (checkbox.checked){
                            id.push(checkbox.value)
                        }
                    }
                    var model = JSON.stringify({ userId: id, roleId: roleId });
                    console.log(id)
                      var token = localStorage.getItem("token")
                      $.ajax({
                        url: '../User/UseRoleAddDo',
                          type: 'post',
                         data: { model: model, token: token },
                          async: false,
                          //contentType: 'application/json',
                          success: function (res) {
                              var res = JSON.parse(res)
                              console.log(res)
                             if(res.code==200)
                             {
                                 alert("添加成功")
                             }
                             else{
                                 alert("添加失败")
                             }
                          }
                      })
                }
            }

            $(function () {
                UserBang();
                RoleBang();
            })
        });

    </script>

</body>
</html>